package siwi.map.android;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class lvButtonAdapter extends BaseAdapter { 
	private final static String TAG = "lvButtonAdapter";
    private class buttonViewHolder { 
    	TextView appIcon; 
        TextView appName; 
        ImageButton view_btn; 
    } 
    
    private ArrayList < HashMap < String , Object > > mAppList; 
    private LayoutInflater mInflater; 
    private Context mContext; 
    private String [ ] keyString; 
    private int [ ] valueViewID; 
    private buttonViewHolder holder; 
    
    public lvButtonAdapter( Context c, ArrayList < HashMap < String , Object > > appList, int resource, 
            String [ ] from , int [ ] to) { 
        mAppList = appList; 
        mContext = c; 
        mInflater = ( LayoutInflater) mContext. getSystemService( Context . LAYOUT_INFLATER_SERVICE) ; 
        keyString = new String [ from . length ] ; 
        valueViewID = new int [ to. length ] ; 
        System . arraycopy ( from , 0, keyString, 0, from . length ) ; 
        System . arraycopy ( to, 0, valueViewID, 0, to. length ) ; 
    } 
    
    @Override 
    public int getCount ( ) { 
        return mAppList. size ( ) ; 
    } 

    @Override 
    public Object getItem ( int position ) { 
        return mAppList. get ( position ) ; 
    } 

    @Override 
    public long getItemId( int position ) { 
        return position ; 
    } 

    public void removeItem ( int position ) { 
        mAppList. remove ( position ) ; 
        this . notifyDataSetChanged( ) ; 
    } 
    
    @Override 
    public View getView ( int position , View convertView, ViewGroup parent ) {
    	
        if ( convertView != null ) { 
            holder = ( buttonViewHolder) convertView. getTag ( ) ; 
        } else { 
            convertView = mInflater. inflate ( R. layout.order, null ) ; 
            holder = new buttonViewHolder( ) ; 
            holder. appIcon = ( TextView ) convertView. findViewById( valueViewID[ 0] ) ; 
            holder. appName = ( TextView) convertView. findViewById( valueViewID[ 1] ) ; 
            convertView. setTag( holder) ; 
        } 
        
        HashMap < String , Object > appInfo = mAppList. get ( position ) ;
//        Log.v(TAG, keyString[0] + " - "+ appInfo.get(keyString[0]) + ", " + keyString[1]);
        if ( appInfo != null ) { 
            String aname = ( String ) appInfo. get ( keyString[0] ) ; 
            holder. appName. setText ( aname) ; 
            holder. appIcon. setText(aname ) ;  
        }     

        holder.view_btn = (ImageButton) convertView.findViewById(R.id.order_img_btn);
		holder.view_btn.setOnClickListener(new Button.OnClickListener(){
			@Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
//				Log.v(Constant.MARS, "onClick");
            }});
		
        return convertView; 
    } 

    class lvButtonListener implements OnClickListener { 
        private int position ; 

        lvButtonListener( int pos) { 
            position = pos; 
        } 
        
		@Override
		public void onClick(DialogInterface arg0, int arg1) {
            int vid= ((View)arg0). getId ( ) ; 
            if ( vid == holder.view_btn. getId ( ) ) 
                removeItem ( position ) ; 
		} 
    } 
}
 